/**
 * Copyright (c) 2013 Nokia Corporation. All rights reserved.
 * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation. 
 * Oracle and Java are trademarks or registered trademarks of Oracle and/or its
 * affiliates. Other product and company names mentioned herein may be trademarks
 * or trade names of their respective owners. 
 * See LICENSE.TXT for license information.
 */

package com.nokia.example.composedui.views;

import java.util.Vector;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;

import com.nokia.example.composedui.ViewManager;
import com.nokia.example.composedui.model.DataItem;
import com.nokia.example.composedui.model.ItemStorage;
import com.nokia.example.composedui.model.ItemStorageListener;
import com.nokia.example.composedui.utils.AppDataStorage;
import com.nokia.example.composedui.utils.PersistableState;
import com.nokia.example.composedui.views.elements.Commands;

/**
 * The view for displaying the data items in a basic list.
 */
public class ListView
    extends List
    implements CommandListener,
               ItemStorageListener,
               PersistableState
{
    // Members
    private final Command addCommand;
    private final Command deleteMultipleCommand;
    private final Command exitCommand;
    private ViewManager viewManager;
    private ItemStorage itemStorage;

    /**
     * Constructor.
     */
    public ListView() {
        super("List view", List.IMPLICIT);
        addCommand = new Command("Add new", Command.SCREEN, 1);
        deleteMultipleCommand = new Command("Delete items", Command.SCREEN, 1);
        exitCommand = new Command("Exit", Command.EXIT, 1);
        addCommand(addCommand);
        addCommand(deleteMultipleCommand);
        addCommand(exitCommand);
        setCommandListener(this);
    }

    /**
     * Constructor.
     * @param itemStorage
     * @param viewManager
     */
    public ListView(ItemStorage itemStorage, ViewManager viewManager) {
        this();
        restoreInstance(null, itemStorage, viewManager, null, null);
    }

    /**
     * @see PersistableState#restoreInstance(DataItem, ItemStorage, ViewManager, AppDataStorage, String)
     */
    public void restoreInstance(DataItem selectedDataItem,
                                ItemStorage itemStorage,
                                ViewManager viewManager,
                                AppDataStorage appDataStorage,
                                String uniqueId)
    {
        this.viewManager = viewManager;
        this.itemStorage = itemStorage;
        this.itemStorage.addItemStorageListener(this);
    }

    /**
     * @see PersistableState#saveInstance(AppDataStorage, String)
     */
    public void saveInstance(AppDataStorage appDataStorage, String uniqueId) {
        // No implementation required
    }

    /**
     * @see ItemStorageListener#dataChanged(Vector)
     */
    public void dataChanged(Vector dataItems) {
        populateList(dataItems);
    }

    /**
     * @see javax.microedition.lcdui.CommandListener#commandAction(Command, Displayable)
     */
    public void commandAction(Command command, Displayable displayable) {
        if (command == Commands.LIST_SELECT) {
            // Some list item was selected
            int index = getSelectedIndex();
            DataItem selected = itemStorage.elementAt(index);
            viewManager.showView(new ItemView(selected, itemStorage, viewManager));
        }
        else if (addCommand.equals(command)) {
            viewManager.showView(new ItemEditView(itemStorage, viewManager));
        }
        else if (deleteMultipleCommand.equals(command)) {
            viewManager.showView(new ItemDeleteView(itemStorage, viewManager));
        }
        else if (exitCommand.equals(command)) {
            viewManager.goBack();
        }
    }

    /**
     * Populates the list with the given data items.
     * @param dataItems The data items to display in the view.
     */
    private void populateList(Vector dataItems) {
        deleteAll();
        
        for (int i = 0; i < dataItems.size(); i++) {
            DataItem item = (DataItem)dataItems.elementAt(i);
            append(item.getName(), null);
        }
    }
}